package resources;



//File Cleaner
public class FileCleaner {
	private FileReaderWriter file = new FileReaderWriter();
	
	public FileCleaner() {}
	
	public boolean clean(String filename) {
		// variables
		int endIndex = 0;
		int startIndex = 0;
		String newLine = "\n";
		String line = "";
		// read file
		StringBuilder data = file.ReadFile(filename);
		// remove comments on top.
		String check1 = "FORMER FELLOWS OF THE ROYAL SOCIETY OF EDINBURGH";
		data.delete(0, data.indexOf(check1));
		// delete the comments at the end of the file
		data.delete(data.indexOf("ISBN"), data.length());
		// remove all the title page references
		while (data.indexOf(check1) != -1) {
			int beginIndex = data.indexOf(check1);
			endIndex = data.indexOf("\n", beginIndex);
			data.delete(beginIndex, endIndex + 3);
		}
		// fix the records
		endIndex = 0;
		startIndex = 0;
		// start the repair
		while (endIndex != data.length() - 1) {
			endIndex = data.indexOf(newLine, startIndex);
			line = (String) data.subSequence(startIndex, endIndex);
			if (line.contains("Ref:")) {
						data.insert(startIndex, "\n");
			}	else  if (line.contains("See")&!line.contains("See Notes")){
							data.insert(startIndex, "\n\n");
							data.insert(endIndex, "\n");
				}
			if (!isValidInfo(line) & !line.contains("Ref:")) {
					if (startIndex==0) startIndex=1;
					data.deleteCharAt(startIndex-1);
					data.insert(startIndex-1, " ");
			}
			startIndex = endIndex + 1;
		}
		file.WriteFile(filename, data.toString());
		data=null;
		
		return true;
	}
	
	private boolean isValidInfo(String line) {
		if (!line.isEmpty()) {
			if (line.contains("Profession:") & line.charAt(0) == 'P')
				return true;
			else if (line.contains("Date of Election:") & line.charAt(0) == 'D')
				return true;
			else if (line.contains("Proposers:") & line.charAt(0) == 'P')
				return true;
			else if (line.contains("Notes:") & line.charAt(0) == 'N')
				return true;
			else if (line.contains("Fellow Type:") & line.charAt(0) == 'F')
				return true;
			else if (line.contains("Appointments Held:")
					& line.charAt(0) == 'A')
				return true;
			else if (line.contains("Undergraduate Studies:")
					& line.charAt(0) == 'U')
				return true;
			else if (line.contains("Postgraduate Studies:")
					& line.charAt(0) == 'P')
				return true;
			else if (line.contains("Marital Status:") & line.charAt(0) == 'M')
				return true;
			else if (line.contains("Family:") & line.charAt(0) == 'F')
				return true;
			else if (line.contains("War Service:") & line.charAt(0) == 'W')
				return true;
			else if (line.contains("Father:") & line.charAt(0) == 'F')
				return true;
			else if (line.contains("References:") & line.charAt(0) == 'R')
				return true;
			else if (line.contains("Memberships:") & line.charAt(0) == 'M')
				return true;
			else if (line.contains("Titles and British Honours:")
					& line.charAt(0) == 'T')
				return true;
			else if (line.contains("Place of Birth:") & line.charAt(0) == 'P')
				return true;
			else if (line.contains("Place of Death:") & line.charAt(0) == 'P')
				return true;
			else if (line.contains("Place of interment:")
					& line.charAt(0) == 'P')
				return true;
			else if (line.contains("Schools and Tutors:")
					& line.charAt(0) == 'S')
				return true;
			else if (line.contains("Mother:") & line.charAt(0) == 'M')
				return true;
			else if (line.contains("Training:") & line.charAt(0) == 'T')
				return true;
			else if (line
					.contains("Resigned/Removed/Election Cancelled/Not Admitted:")
					& line.charAt(0) == 'R')
				return true;
			else if (line.contains("Date of Resignation:")
					& line.charAt(0) == 'D')
				return true;
			else if (line.contains("Service to the RSE:")
					& line.charAt(0) == 'S')
				return true;
			else if (line.contains("Other Family:") & line.charAt(0) == 'O')
				return true;
			else if (line.contains("Publications:") & line.charAt(0) == 'P')
				return true;
			else if (line.contains("RSE Prizes and Medals:")
					& line.charAt(0) == 'R')
				return true;
			else if (line.contains("Siblings:")
					& line.charAt(0) == 'S')
				return true;
			else if (line.contains("Ref:"))
				return true;
			else
				return false;

		} else
			return false;
	}
}
